package com.qk.controller;

import com.qk.aop.anno.LogRecord;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/07 20:59
 * @Description: 角色管理
 */
@RestController
@Slf4j
@RequestMapping("/roles")
@SuppressWarnings("all")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 修改角色
     *
     * @param role
     * @return Result
     */
    @LogRecord
    @PutMapping
    public Result updateRole(@RequestBody Role role) {
        log.info("修改角色，参数：{}", role);
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 根据id查询角色
     *
     * @param id
     * @return Result
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询角色，参数：{}", id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    /**
     * 添加角色
     *
     * @param role
     * @return Result
     */
    @LogRecord
    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("添加角色，参数：{}", role);
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 删除角色
     *
     * @param id
     * @return Result
     */
    @LogRecord
    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        log.info("删除角色，参数：{}", id);
        roleService.deleteRole(id);
        return Result.success();
    }

    /**
     * 获取角色列表
     *
     * @param roleDTO
     * @return Result
     */
    @GetMapping
    public Result getByPage(RoleDTO roleDTO) {
        PageResult<Role> list = roleService.getByPage(roleDTO);
        return Result.success(list);
    }

    /**
     * 获取角色列表
     *
     * @return Result
     */
    @GetMapping("/list")
    public Result getList() {
        List<Role> list = roleService.getList();
        return Result.success(list);
    }

}
